package com.exam.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.demo.common.Result;
import com.exam.demo.entity.Menu;
import com.exam.demo.entity.Paper;
import com.exam.demo.entity.Question;
import com.exam.demo.service.QuestionService;
import com.exam.demo.util.PaperUtil;
import com.exam.demo.util.generic.GA;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static java.time.Clock.system;

/**
 * 功能描述
 *
 * @author yxj30
 * @date 2022/03/16  19:18
 */
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Resource
    QuestionService questionService;

    //查找页面数据
    @GetMapping
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String questionTitle,
                           @RequestParam(defaultValue = "0") Integer questionType,
                           @RequestParam(defaultValue = "0") String courseId,
                           @RequestParam(defaultValue = "0") String teacherId
    ) {
        System.out.println("questionType = " + questionType);
        Page<Question> page =  questionService.findPage(new Page<Question>(pageNum, pageSize),
                questionTitle,questionType,courseId,teacherId);
        return Result.success(page);
    }

    //更改和插入
    @PostMapping("/save")
    public Result insert(@RequestBody Question question) {
        return Result.success(questionService.saveOrUpdate(question));
    }
    //删除
    @DeleteMapping("/delete/{questionId}")
    public Result delete(@PathVariable Integer questionId) {
        return Result.success(questionService.removeById(questionId));
    }
    /**
     * excel 导入
     *
     * @param file
     * @throws Exception
     */
    @PostMapping("/import")
    public Result imp(MultipartFile file) throws Exception {
        boolean b = questionService.imp(file);
        return Result.success(b); //false导入失败
    }
    /**
     * 导出接口
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        response = questionService.export(response);
    }

    /**
     * 随机组卷
     * 获取一系列试题
     * 有问题，应该按难度选题
     *
     * @param paper 试卷
     */
    @PostMapping("/randomPaper")
    public Result random(@RequestBody Paper paper) {
        System.out.println(paper);
        ArrayList<Question> list =  questionService.random(paper);
        return Result.success(list);
    }
    //查找根据courseid全部专业数据
    @GetMapping("/find/{courseId}")
    public Result findPage(@PathVariable String courseId){
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
//        Question course_id = questionService.getOne(queryWrapper.eq("course_id", courseId));
        queryWrapper.select("question_type as questionType ,COUNT(question_type) as count");
        queryWrapper.eq("course_id",courseId);
        queryWrapper.groupBy("question_type");

        List<Map<String, Object>> maps = questionService.listMaps(queryWrapper);
        for (Map map:maps) {
            System.out.println(map.get("questionType"));
            System.out.println(map.get("count"));
        }
        List<Object> objects = questionService.listObjs(queryWrapper);
        return Result.success(maps);
    }

    //查找选择页面数据
    @GetMapping("/selectQuestion")
    public Result findPageByQuestionTypeAndQuestionTitle(@RequestParam Integer pageNum,
                                                         @RequestParam Integer pageSize,
                                                         @RequestParam(defaultValue = "") String questionTitle,
                                                         @RequestParam Integer questionType) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("question_title", questionTitle);
        System.out.println("questionType = " + questionType);
        if(questionType == null){
        }else{
            queryWrapper.like("question_type", questionType);
        }
        Page<Question> page =  null;
        return Result.success(page);
    }











}
